using AppToolKit.Model.i18n;
using System;
using System.Text;
namespace AppToolKit.Model.Validation.Validator
{
	public class StringLenRuleValidator : DefalutValidator
	{
		private int len_;
		public StringLenRuleValidator(int len)
		{
			this.len_ = len;
		}
		public override ValidateResult Validate(ValidateContext context)
		{
			string text = context.NewValue.ToString().Trim();
			ValidateResult result;
			if (!string.IsNullOrEmpty(text))
			{
				if (Encoding.Default.GetByteCount(text) > this.len_)
				{
					result = ValidateResult.Error(string.Format(StringResource.StrLenValidateMessage, context.PropertyDescript, this.len_));
				}
				else
				{
					if (text.Length > this.len_)
					{
						result = ValidateResult.Error(string.Format(StringResource.StrLenValidateMessage, context.PropertyDescript, this.len_));
					}
					else
					{
						result = null;
					}
				}
			}
			else
			{
				result = null;
			}
			return result;
		}
	}
}
